<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@page import="com.carnation.utils.SessionInfo" %>
<%@ include file="../taglibs.jsp" %>
<%@ include file="../easyuiLib.jsp" %>
<%SessionInfo sessionInfo = (SessionInfo)request.getSession().getAttribute("sessionInfo");%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" type="text/css" href="css/zidingyi.css" />
<script type="text/javascript" charset="utf-8">
function logout(){
	$.ajax({
		url:'${ctx}/Logout',
		cache:false,
		success:function(data){
			var json = JSON.parse(data);
			if(json.status == "200"){
				location.replace("${ctx}/login.jsp");
			}
			else{
				$.messager.alert("标题","发送未知错误,请联系系统管理员");
			}
		}
	});
}
</script>
<script type="text/javascript" charset="utf-8">
var request = new XMLHttpRequest();
var hd;
var _data;
function hello(){
	if(request.readyState == 4 && request.status == 200){
		var test = request.responseText;
		var json = eval('(' + test+ ')');
		hd = json.head;
		console.log(hd);
		_data = json.content;
		console.log(_data);
		tabClose();
		tabCloseEven();
		clearHeadMenu('headmenu','li');
		setHeadMenu('headmenu',hd);
		$('#headmenu a').click(function(){
			$('#headmenu a').removeClass('active');
			$(this).addClass('active');
			var data = _data[$(this).attr('name')];
			clearNav();
			addNav(data);
			InitLeftMenu();
		});
		
		$('#lead').accordion({
			animate:false
		});
		var firstMenueName = $('#headmenu a').attr('name');
		console.log(_data[firstMenueName]);
		addNav(_data[firstMenueName]);
		InitLeftMenu();
	}
}
$(function(){
	//发送请求
	request.onreadystatechange = hello;
	request.open("POST","Init",true);
	request.send(null);
});

function setHeadMenu(id,data){
	var objId = "#" + id;
	var json = data;
	for(var i =0;i<json.length;i++){
		var liContent = "";
		liContent += '<li>';
		liContent += '<div style="border:1px solid #99BBE8; background:#E0ECFF;cursor:pointer;width:80px;text-align:center"><a name="'+ json[i].name+ '" title="' + json[i].title + '" >' + json[i].text + '</a></div>'; 
		liContent += '</li>';
		$(objId).append(liContent);
	}	
}

function clearHeadMenu(id,objName){
	var objId = "#" + id;
	$(objId + " "+ objName).remove();
}

function createFrame(url){
	var s='<iframe scrolling="auto" frameborder="0" src="' + url + '" style="width:100%;height:100%"><iframe>';
	return s;
}

function addNav(data){
	console.log(data.length);
	$.each(data,function(i,sm){
		var menulist ="";
		menulist +='<ul>';
		$.each(sm.menuList,function(j,o){
			menulist += '<li><div><a ref="' + o.id + '" href="#" rel="${ctx}'
			+ o.url + '" ><span class="icon ' + o.icon
			+ '" >&nbsp;</span><span class="nav">' + o.name
			+ '</span></a></div></li> ';
		});
		menulist +='</ul>';
		$('#lead').accordion('add',{
			title :sm.name,
			content : menulist,
			iconCls : 'icon ' + sm.icon 
		});
	});
	var pp = $('#lead').accordion('panels');
	var t = pp[0].panel('options').title;
	$('#lead').accordion('select',t);
}

function clearNav(){
	var pp = $('#lead').accordion('panels');
	$.each(pp,function(i,n){
		if(n){
			var t= n.panel('options').title;
			$('#lead').accordion('remove',t);
		}
	});
	pp = $('#lead').accordion('getSelected');
	if(pp){
		var title = pp.panel('options').title;
		$('#lead').accordion('remove',title);
	}
}

function addTab(subtitle,url){
	if(!$('#tabs').tabs('exists',subtitle)){
		$('#tabs').tabs('add',{
			title:subtitle,
			content : createFrame(url),
			closable :true,
		});
	}
	else {
		$('#tabs').tabs('select',subtitle);
		$('#mm-tabuodate').click();
	}
	tabClose();
}

function tabClose(){
	$(".tabs-inner").dblclick(function(){
		var subtitle = $(this).children(".tabs-closable").text();
		if(subtitle != "欢迎使用"){
			$('#tabs').tabs('close',subtitle);
		}
	});
	
	$(".tabs-inner").bind("contextmenu",function(e){
		$('#mm').menu('show',{
			left:e.pageX,
			top:e.pageY
		});
		var subtitle = $(this).children(".tabs-closable").text();
		$('#mm').data("currtab",subtitle);
		$('#tabs').tabs('select',subtitle);
		return false;
	});
}

function tabCloseEven(){
	$('#mm-tabupdate').click(function() {
		var currTab = $('#tabs').tabs('getSelected');
		if(currTab.panel('options').title != "欢迎使用"){
			var url = $(currTab.panel('options').content).attr('src');
			$('#tabs').tabs('update', {
				tab : currTab,
				options : {
					content : createFrame(url)
				}
			});
		}
	});
	
	$('#mm-tabcloseall').click(function(){
		var pp = $('#tabs').tabs('tabs');
		for(var i=pp.length - 1;i>=0;i--){
			var t =pp[i].panel('options').title;
			if(t != "欢迎使用"){
				$('#tabs').tabs('close',t);
			}
		}
	});
	
	$('#mm-tabclose').click(function(){
		var currtab = $('#mm').data("currtab");
		$('#tabs').tabs('close',currtab);
	});
	
	$('#mm-tabcloseother').click(function(){
		var currtab_title=$('#mm').data("currtab");
		var pp = $('#tabs').tabs('tabs');
		for(var i=pp.length - 1 ;i>= 0 ;i--){
			var t =pp[i].panel('options').title;
			if(t != "欢迎使用" && t != currtab_title){
				$('#tabs').tabs('close',t);
			}
		}
	});
}

function InitLeftMenu(){
	hoverMenuItem();
	
	$('#lead li a').on('click',function(){
		var tabTitle = $(this).children('.nav').text();
		var url = $(this).attr("rel");
		addTab(tabTitle,url);
		$('#lead li div').removeClass("selected");
		$(this).parent().addClass("selected");
	});
}

function hoverMenuItem(){
	$(".easyui-accordion").find('a').hover(function(){
		$(this).parent().addClass("hover");}
	,function(){
			$(this).parent().removeClass("hover");
		}
	);
}
</script>
<title>试用版下载平台</title>
</head>
<body class="easyui-layout" style="overflow-y:hidden" scroll="no">
	<div region="north" split="true" border="false" style="overflow:hidden;height:70px;background-color:#F1F1F1;line-height:20px;color:#fff;font-family:Verdana,微软雅黑,黑体">
		<span style="float:right;padding-right:20px;color:blue;" class="head"><span>欢迎使用下载平台 用户:</span><span style="text-decoration:underline"><%=sessionInfo.getLoginName() %></span><br /><span style="float:right"><a href="javascript:void(0);" onclick="logout()">安全退出</a></span></span>
		<span style="float:left;padding-left:0px;font-size:16px;">
			<img src="images/logo.png" width="529" height="66"/>
		</span>
		<ul class="test" id="headmenu" style="padding:0px;font-size:16px;">
			
		</ul>
	</div>
	<div region="south" split="true" style="height:30px;background:#D2E0F2;">
		<div style="text-align:center;font-size:15px;color:blue;font-weight:bold;"><span>By xiemb Email:<a href="mailto:xiemb@carnation.com.cn">xiemb@carnation.com.cn</a></span></div>
	</div>
	<div region="west" hide="true" split="true" title="导航菜单" style="width:180px" id="west">
		<div id="lead" class="easyui-accordion" fit="true" border="false" >
			
		</div>
	</div>
	<div id="mainPanel" region="center" style="background:#eee;overflow-y:hidden">
		<div id="tabs" class="easyui-tabs" fit="true" border="false">
			<div title="欢迎使用" style="padding:20px;overflow:hidden;" id="home">
				<h1>欢迎使用试用版下载平台</h1>
			</div>
		</div>
	</div>
	<div id="mm" class="easyui-menu" style="width:150px">
		<div id="mm-tabupdate">刷新</div>
		<div id="mm-tabclose">关闭</div>
		<div id="mm-tabcloseall">全部关闭</div>
		<div id="mm-tabcloseother">除此之外全部关闭</div>
	</div>
</body>
</html>